#########################
#   Author : Leon yu    #
#   Date : 2025/06/14   #
#   Id : SM2772         #
#########################
from apiRespsAnalyze import *

# 数据解析
def querySalesItemReportResult(startTime, endTime):
    result = dict()
    # 总销售额
    result['SALES_AMOUNT'] = 0
    # 毛利润总和
    result['TOTAL_GP'] = 0
    # 毛利率
    # result['GP_RATE'] = ""
    # 商品销售明细 列表
    result['salesReportModelList'] = list()
    
    querySalesItemReport = analyzeQuerySalesItemReport(startTime=startTime, endTime=endTime)
    statisticalData = querySalesItemReport['statisticalData']
    salesReportModelList = querySalesItemReport['salesReportModelList']
    if len(statisticalData) > 0:
        for item in statisticalData:
            result['SALES_AMOUNT'] = item['value']['amount'] if item['key'] == 'SALES_AMOUNT' else result['SALES_AMOUNT']
            result['TOTAL_GP'] = item['value']['amount'] if item['key'] == 'TOTAL_GP' else result['TOTAL_GP']
            # result['GP_RATE'] = item['value'] if item['key'] == 'GP_RATE' else result['GP_RATE']
    
    if len(salesReportModelList) > 0:
        salesReportModelList = [
                {
                    item['skuId']: {
                        'salesPrice' : item['salesPrice']['amount'],
                        'sales' : item['sales'],
                        'gp' : item['gp']['amount'],   # 毛利润
                        # 'gpRate' : item['gpRate']      # 毛利率
                        }
                    } 
                for item in salesReportModelList
            ]
        result['salesReportModelList'] = salesReportModelList
    return result
